﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using ColdSun.Framework;
using ColdSun.Commons.Entities;

namespace ColdSun.Commons
{
    public class Utilities
    {
        public static bool IsIncludeString(string value, string[] valueList)
        {
            if (string.IsNullOrEmpty(value) || valueList == null)
            {
                return false;
            }
            foreach (string index in valueList)
            {
                if (index.Trim().EndsWith(value.Trim(), StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }

        public static string Cell(string columnName, int rowIndex)
        {
            return string.Format("{0}{1}", columnName.ToUpper(), rowIndex);
        }

        public static string PaddingValue(string value, char paddingChar, int formatLength, bool isLeftSide)
        {
            if (value.Length > formatLength)
            {
                return value;
            }

            string addLength = new string(paddingChar, formatLength - value.Length); ;
            if (isLeftSide)
            {
                value = addLength + value;
            }
            else
            {
                value += addLength;
            }

            return value;
        }

        public static string GetFullFileName(string fileName)
        {
            if (string.IsNullOrEmpty(fileName) || fileName.Contains(":\\"))
            {
                return fileName;
            }
            if (!fileName.StartsWith("\\"))
            {
                fileName = "\\" + fileName;
            }
            string fullFileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + fileName;
            if (fullFileName.StartsWith("file:\\"))
            {
                fullFileName = fullFileName.Remove(0, "file:\\".Length);
            }
            return fullFileName;
        }

        public static List<Replacement> GetReplacementInfo(string infoFile)
        {
            try
            {
                Logger.Info(string.Format("Get info data from info file: {0}", infoFile));
                TextReader tr = new StreamReader(infoFile);
                string xmlString = tr.ReadToEnd();
                return XmlUtility.DeserializeObject<List<Replacement>>(xmlString);
            }
            catch (Exception ex)
            {
                Logger.Error("Get info data from info file", ex);
            }
            return null;
        }

        public static DateTime GetFirstDayOfWeek(DateTime dayInWeek)
        {
            DateTime firstDayInWeek = dayInWeek.Date;
            while (firstDayInWeek.DayOfWeek != DayOfWeek.Monday)
            {
                firstDayInWeek = firstDayInWeek.AddDays(-1);
            }
            return firstDayInWeek;
        }

        public static DateTime GetLastDayOfWeek(DateTime dayInWeek)
        {
            DateTime firstDayInWeek = GetFirstDayOfWeek(dayInWeek);
            DateTime lastDayInWeek = firstDayInWeek.AddDays(5);
            //if (lastDayInWeek.Month != firstDayInWeek.Month)
            //{
            //    lastDayInWeek = new DateTime(lastDayInWeek.Year, lastDayInWeek.Month, 1);
            //    lastDayInWeek.AddDays(-1);
            //}
            return lastDayInWeek;
        }

        public static string ChangeMoneyToText(object money)
        {
            if (money == null) return "";
            string moneyString = PaddingValue(money.ToString(), '0', 12, true);
            List<string> moneyParts = new List<string>();
            string[] columnValues = new string[] { "tỷ", "triệu", "nghìn", "" };
            string result = "";
            for (int index = 0; index < 4; index++)
            {
                string changePart = ChangeMoneyToTextWith3Digits(moneyString.Substring(index * 3, 3), index, result.Trim()).Trim();
                if (changePart != string.Empty)
                {
                    //changePart += " " + columnValues[index] + ",";
                    changePart += " " + columnValues[index];
                }
                result += " " + changePart;
                moneyParts.Add(changePart);
            }
            result = result.Trim().Replace("  ", " ") + " đồng";
            result = result[0].ToString().ToUpper() + result.Substring(1);
            //if (result.EndsWith(","))
            //{
            //    result = result.Remove(result.Length - 1);
            //}
            return result;
        }

        private static string ChangeMoneyToTextWith3Digits(string value, int index, string previuosResult)
        {
            string[] columnNumbers = new string[] { "", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
            string[] columnNumbers1 = new string[] { "", "một", "hai", "ba", "bốn", "lăm", "sáu", "bảy", "tám", "chín" };
            string[] columnNumbers2 = new string[] { "", "mốt", "hai", "ba", "bốn", "lăm", "sáu", "bảy", "tám", "chín" };
            
            int hunred = Convert.ToInt16(value.Substring(0, 1));
            int unit = Convert.ToInt32(value.Substring(2, 1));
            int tens = Convert.ToInt32(value.Substring(1, 1));

            string result = "";

            if (previuosResult.Trim() != "" && hunred == 0 & (tens > 0 || unit > 0))
            {
                result = "không";
            }
            else if (hunred > 0)
            {
                result = columnNumbers[hunred];
            }
            if (!string.IsNullOrEmpty(result))
            {
                result += " trăm";
            }
            
            switch (tens)
            {
                case 0:
                    if ((index == 3 && unit > 0 && previuosResult != "")
                        || ( unit > 0 && hunred > 0))
                    { 
                        result += " lẻ " + columnNumbers[unit]; 
                    }
                    else { result += " " + columnNumbers[unit]; }
                    break;
                case 1:
                    result += " mười " + columnNumbers1[unit];
                    break;
                default:
                    result += " " + columnNumbers[tens] + " mươi " + columnNumbers2[unit];
                    break;
            }
            return result;
        }
    }
}
